﻿using Advantech.Adam;
using EONFULLSIN2.Core.Extensions;
using EONFULLSIN2.Models.Attributes;
using EONFULLSIN2.Models.Dto.Device;
using EONFULLSIN2.Models.Enum;
using EONFULLSIN2.Models.Enum.IOPointEnum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EONFULLSIN2.Services.Device.DIOModelA
{
    /// <summary>
    /// IO模块A
    /// </summary>
    public class AdamComDIOModelAService : AdamComBaseServices, IAdamComDIOModelAService
    {
        public AdamComDIOModelAService()
        {
            this.AdamIOPoints = Enum.GetValues(typeof(eIOPointEnum))
                                    .Cast<eIOPointEnum>()
                                    .Where(o => o.GetAttributeOfType<IOPointAttribute>().AdamModelType == eAdamModelType.Model_A)
                                    .ToList();
        }

        public override string COMName { protected set; get; } = "COM1";

        public override List<eIOPointEnum> AdamIOPoints { protected set; get; }

        public override Task<(bool DigitalInputRlt, List<RelayChannelItemDto> ChannelStatusList)> ReadDOAllChannelStatusAsync()
        {
            (bool digitalInputRlt, List<RelayChannelItemDto> channelStatusList) res = (false, new List<RelayChannelItemDto>());
            bool[] bDI, bDO;
            var readRes = adamCom.DigitalInput(m_iAddr).GetValues(m_iDITotal, m_iDOTotal, out bDI, out bDO);
            res.digitalInputRlt = readRes;
            if (readRes)
            {
                for (int i = 0; i < m_iDOTotal; i++)
                {
                    var adamIOPointType = AdamIOPoints.FirstOrDefault(o => o.GetAttributeOfType<IOPointAttribute>().ChannelIndex == i);
                    var value = new RelayChannelItemDto()
                    {
                        ChannelIndex = i,
                        IOPointType = adamIOPointType,
                        ChannelStatus = bDO[i]
                    };
                    res.channelStatusList.Add(value);
                }
            }
            return Task.FromResult(res);
        }

    }
}
